{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c4766a84",
   "metadata": {},
   "source": [
    "1. 简答题\n",
    "上机3-03：\n",
    "\n",
    "创建类SchoolKid，初始化小孩的姓名、年龄。也有访问每个属性的方法和修改属性的方法。然后创建类ExaggeratingKid，继承类SchoolKid，子类中覆盖访问年龄的方法，并将实际年龄加2。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "affe3595",
   "metadata": {},
   "source": [
    "上机3-04：\n",
    "\n",
    "有五种鲜花，花名和价格，分别是：\n",
    "\n",
    "（1）牵牛花（petunia）：50\n",
    "\n",
    "（2）三色堇（pansy）：75\n",
    "\n",
    "（3）玫瑰（rose）：15\n",
    "\n",
    "（4）紫罗兰（violet）：50\n",
    "\n",
    "（5）康乃馨（carnation）：80\n",
    "\n",
    "编写一个类，并在程序中实例化该类，调用类中的方法，实现输入购买数量和花名，就能打印出总价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ace07005",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "class SchoolKid:\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name  # 初始化姓名\n",
    "        self.age = age  # 初始化年龄\n",
    "\n",
    "    def change_name(self, new_name):\n",
    "        self.name = new_name  # 修改姓名\n",
    "        return self.name\n",
    "\n",
    "    def change_age(self, new_age):\n",
    "        self.age = new_age  # 修改年龄\n",
    "        return self.age\n",
    "\n",
    "    def get(self, attr=\"name\"):\n",
    "        if attr == \"name\":\n",
    "            return self.name  # 访问姓名\n",
    "        elif attr == \"age\":\n",
    "            return self.age  # 访问年龄\n",
    "        else:\n",
    "            print(\"属性不存在:\", attr)\n",
    "            return None\n",
    "\n",
    "\n",
    "class ExaggeratingKid(SchoolKid):\n",
    "    def get(self, attr=\"name\"):\n",
    "        if attr == \"name\":\n",
    "            return self.name\n",
    "        elif attr == \"age\":\n",
    "            return self.age + 2  # 覆盖父类方法，年龄加2\n",
    "        else:\n",
    "            print(\"属性不存在:\", attr)\n",
    "            return None\n",
    "\n",
    "\n",
    "# 创建SchoolKid实例\n",
    "kid1 = SchoolKid(\"小明\", 10)\n",
    "print(kid1.get(\"age\"))  # 输出: 10\n",
    "\n",
    "# 创建ExaggeratingKid实例\n",
    "kid2 = ExaggeratingKid(\"小红\", 10)\n",
    "print(kid2.get(\"age\"))  # 输出: 12（年龄加2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6a8d808c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total price for 2 rose(s) is: 30 yuan\n"
     ]
    }
   ],
   "source": [
    "class FlowerShop:\n",
    "    def __init__(self):\n",
    "        # 初始化鲜花价格字典，键为英文花名，值为价格\n",
    "        self.flower_prices = {\n",
    "            \"petunia\": 50,\n",
    "            \"pansy\": 75,\n",
    "            \"rose\": 15,\n",
    "            \"violet\": 50,\n",
    "            \"carnation\": 80\n",
    "        }\n",
    "    \n",
    "    def calculate_total(self, flower_name, quantity):\n",
    "        # 方法：根据花名和数量计算总价\n",
    "        flower_name_lower = flower_name.lower()  # 统一转换为小写，避免大小写问题\n",
    "        if flower_name_lower in self.flower_prices:\n",
    "            price_per_unit = self.flower_prices[flower_name_lower]\n",
    "            total_price = price_per_unit * quantity\n",
    "            print(f\"Total price for {quantity} {flower_name_lower}(s) is: {total_price} yuan\")\n",
    "        else:\n",
    "            print(\"Error: Flower name not found. Please choose from: petunia, pansy, rose, violet, carnation.\")\n",
    "\n",
    "# 主程序\n",
    "if __name__ == \"__main__\":\n",
    "    # 实例化FlowerShop类\n",
    "    shop = FlowerShop()\n",
    "    \n",
    "    # 获取用户输入\n",
    "    try:\n",
    "        flower_name = input(\"Please enter the flower name (e.g., petunia, pansy, rose, violet, carnation): \")\n",
    "        quantity = int(input(\"Please enter the quantity: \"))\n",
    "        # 调用方法计算并打印总价\n",
    "        shop.calculate_total(flower_name, quantity)\n",
    "    except ValueError:\n",
    "        print(\"Invalid input. Please enter a valid integer for quantity.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
